//
//  CSPayListView.m
//  SHCloudStore
//
//  Created by SH_iOS on 2017/4/12.
//  Copyright © 2017年 SH. All rights reserved.
//
#define  KCELLHEIGHT __I6Height(50.f)
#define  KMYBGVIEWHEIGHT __I6Height(210.f)
#import "CSPayListView.h"
#import "CSPaymentWayCell.h"
#import "CSCustomPayWaysModel.h"
@interface CSPayListView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *myBgView;
@property(nonatomic,strong)UITableView *customTableView;
@property(nonatomic,strong)UIButton *dissmissButton;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;

@end
@implementation CSPayListView
// 初始化
- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataSourceArray{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        self.dataSourceArray = [NSMutableArray arrayWithCapacity:0];
        self.dataSourceArray = dataSourceArray;

        
    }
    return  self;
}

- (UIView *)myBgView{
    
    if (!_myBgView) {
        _myBgView = [[UIView alloc]initWithFrame:CGRectMake(0,KScreenHeight, KScreenWidth,(KCELLHEIGHT * (self.dataSourceArray.count + 1 ) + __I6Height(10)) )];
        _myBgView.backgroundColor = KBASEBGCOLOR;
        [self addSubview:_myBgView];
        
    }
    
    return _myBgView;
}
//懒加载 支付类型列表
- (UITableView *)customTableView{
    
    if (!_customTableView) {
        _customTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _customTableView.scrollEnabled = NO;
        _customTableView.showsVerticalScrollIndicator = NO;
        _customTableView.delegate = self;
        _customTableView.dataSource = self;
        _customTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.myBgView addSubview:_customTableView];
        [_customTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.myBgView.mas_top).with.offset(__I6Height(0));

            make.width.mas_equalTo(KScreenWidth);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.bottom.mas_equalTo(self.dissmissButton.mas_top).with.offset(__I6Height(-10));
        }];
    }
    return  _customTableView;
}

//懒加载 取消按钮
- (UIButton *)dissmissButton{
    
    if (!_dissmissButton) {
        _dissmissButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dissmissButton setTitle:@"取消" forState:UIControlStateNormal];
        [_dissmissButton setTitleColor:KTEXTCOLOR  forState:UIControlStateNormal];
        _dissmissButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        _dissmissButton.backgroundColor = [UIColor whiteColor];
        _dissmissButton.layer.borderWidth = 0.5;
        _dissmissButton.layer.borderColor = KLINECOLOR.CGColor;
        [_dissmissButton addTarget:self action:@selector(dissMissCustomView) forControlEvents:UIControlEventTouchUpInside];
        [self.myBgView addSubview:_dissmissButton];
        
        [_dissmissButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX);
            make.bottom.mas_equalTo(self.mas_bottom).with.offset(0);
            make.width.mas_equalTo(KScreenWidth);
            make.height.mas_equalTo(KCELLHEIGHT);
            
        }];
    }
    return  _dissmissButton;
}
// 添加视图
- (void)addSubViews{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self myBgView];
    [self customTableView];
    [self dissmissButton];
    
    
}

//显示视图
- (void)showCustomView{
    [self addSubViews];

    CGRect frame = self.myBgView.frame;
    frame.origin.y = KScreenHeight - (KCELLHEIGHT * (self.dataSourceArray.count + 1 ) + __I6Height(10));
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.myBgView.frame = frame;
    } completion:^(BOOL finished) {
        
        
    }];
    
}

//隐藏视图
- (void)dissMissCustomView{
    
   // CGRect frame = self.myBgView.frame;
    //frame =  CGRectMake(0,kSceneViewHeight, KScreenWidth, KCELLHEIGHT * (self.dataSourceArray.count + 1 ) + __I6Height(10));
    [UIView animateWithDuration:0.25 animations:^{
//        self.myBgView.frame = frame;
        self.alpha = 0;

    } completion:^(BOOL finished) {
        [self.customTableView removeFromSuperview];
        [self.dissmissButton removeFromSuperview];
        [self.myBgView removeFromSuperview];
        [self removeFromSuperview];
    }];
    
    
}



#pragma mark ---- UITableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return  self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"pay";
    CSPaymentWayCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CSPaymentWayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID  ];
        
    }
    if (self.dataSourceArray) {
        cell.customPayWaysModel = self.dataSourceArray[indexPath.row];
    }
    cell.selectedImage.hidden = YES;
    return  cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return KCELLHEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_delegate respondsToSelector:@selector(csCustomPaymentWayView:CustomPaymentWay:)]) {
        CSCustomPayWaysModel *model = self.dataSourceArray[indexPath.row];
        [_delegate  csCustomPaymentWayView:self CustomPaymentWay:model.deposit_type];
    }
    
    
    
}

@end
